package com.whfc.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.PageData;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.MathUtil;
import com.whfc.common.util.PageUtil;
import com.whfc.emp.dao.*;
import com.whfc.emp.dto.*;
import com.whfc.emp.entity.*;
import com.whfc.emp.enums.AppAttachmentType;
import com.whfc.emp.manager.TrainBoxManager;
import com.whfc.emp.param.*;
import com.whfc.emp.service.AppTrainService;
import com.whfc.emp.service.AppTrainingCategoriesService;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.fuum.service.SysDictService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClasssName AppTrainingCategoryServiceImpl
 * @Description 人员管理-培训分类
 * @Date 2025/11/26 16:18
 * @Version 1.0
 */
@DubboService(interfaceClass = AppTrainingCategoriesService.class, version = "1.0.0", timeout = 5000)
public class AppTrainingCategoryServiceImpl implements AppTrainingCategoriesService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private AppTrainingCategoryMapper appTrainingCategoryMapper;

    @NacosValue("${emp.enter.train.type.id}")
    private Integer enterTrainTypeId;

    @DubboReference(interfaceClass = SysDictService.class, version = "1.0.0")
    private SysDictService sysDictService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @Override
    public PageData<AppTrainingCategoryDTO> list(AppTrainingCategoryListParam request) {
        logger.info("人员管理-培训分类列表,request:{}", request);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppTrainingCategoryDTO> list = appTrainingCategoryMapper.selectByParam(request);
        PageHelper.clearPage();
        return PageUtil.pageData(PageInfo.of(list));
    }
 

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AppTrainingCategoryAddParam request) {
        logger.info("人员管理-新增培训分类,request:{}", request);
        AppTrainingCategory record = new AppTrainingCategory();
        BeanUtils.copyProperties(request, record);
        appTrainingCategoryMapper.insertSelective(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(AppTrainingCategoryEditParam request) {
        logger.info("人员管理-编辑培训分类,request:{}", request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void del(Integer trainId) {
        logger.info("人员管理-删除培训分类,trainId:{}", trainId);
        appTrainingCategoryMapper.deleteLogicById(trainId);

    }

    @Override
    public Map<Integer, AppTrainingCategoryDTO> getCategoryMap(List<Integer> ids) {
        List<AppTrainingCategoryDTO> list = appTrainingCategoryMapper.selectByIds(ids);
        if (list != null && !list.isEmpty()) {
            return list.stream().collect(Collectors.toMap(AppTrainingCategoryDTO::getId, item -> item));
        }
        return Collections.emptyMap();
    }

    @Override
    public List<AppTrainingCategory> simpleList() {
        return appTrainingCategoryMapper.selectAll();
    }


}
